#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QGridLayout>
#include <QPushButton>
#include <QLabel>
#include <QTableWidgetItem>
#include <QListWidgetItem>
#include <QMessageBox>

#include <QInputDialog>
#include <QButtonGroup>
#include <QProgressBar>
#include <QTimer>
#include <QTime>
#include <QTextCursor>
#include <memory>

#include "puzzlestate.h"
#include "searchalgorithm.h"
#include "astaralgorithm.h"
#include "bfsalgorithm.h"
#include "dfsalgorithm.h"

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void onSizeChanged();
    void onDefaultState();
    void onRandomState();
    void onManualState();
    void onGridButtonClicked();

    void onClearResults();
    void onRunAllAlgorithms();
private slots:
    void onDemoAStarManhattan();
    void onDemoAStarMisplaced();
    void onDemoBFS();
    void onDemoDFS();
    void onDemoStep();
    void onDemoPause();
    void onDemoResume();
    void onDemoStop(); // 演示下一步
    void onClearLog(); // 清空日志

private:
    void setupUI();
    void setupConnections();
    void createPuzzleGrid(QWidget* parent, int size);
    void updatePuzzleDisplay(QWidget* parent, const PuzzleState& state);
    void addResultToTable(const QString& algorithm, const SearchResult& result);
    void enableControls(bool enabled);
    bool checkSolvability();
    SearchAlgorithm* createAlgorithmById(int algorithmId);
    QString getAlgorithmNameById(int algorithmId);
    QString generateResultSummary(const QVector<SearchResult>& results, const QStringList& algorithmNames);
    bool parseStringToPuzzleState(const QString& input, PuzzleState& state);
    bool validatePuzzleString(const QString& input);
    void showStringInputDialog();
    void startAlgorithmDemo(SearchAlgorithm* algorithm, const QString& algorithmName);
    void updateDemoControlButtons();
    void addLogEntry(const QString& message);
    void clearDemoLog();
    QString formatPuzzleState(const PuzzleState& state);
    
    Ui::MainWindow *ui;
    
    // 数码状态
    PuzzleState m_initialState;
    PuzzleState m_goalState;
    PuzzleState m_currentState;
    int m_puzzleSize;
    
    // 网格布局和按钮
    QGridLayout* m_initialGridLayout;
    QGridLayout* m_goalGridLayout;
    QGridLayout* m_currentGridLayout;
    std::vector<std::vector<QPushButton*>> m_initialButtons;
    std::vector<std::vector<QPushButton*>> m_goalButtons;
    std::vector<std::vector<QPushButton*>> m_currentButtons;
    
    // 搜索算法
    std::unique_ptr<SearchAlgorithm> m_algorithm;
    bool m_isSearching;
    
    // 手动输入状态
    bool m_isManualInput;
    bool m_isSettingInitial;
    QString m_dragSourceGrid;
    
    // 演示功能相关
    QTimer* m_demoTimer;
    std::vector<PuzzleState> m_solutionPath;
    int m_currentDemoStep;
    bool m_isDemoRunning;
    QString m_currentAlgorithmName;
    
    // 日志功能相关
    int m_logStepCounter;

};

#endif // MAINWINDOW_H
